﻿using System;
using UnityEditor;
using UnityEngine;


namespace RStudio.UIFramwork.Editor.Utils
{
    /// <summary>
    /// 
    /// </summary>
    public class EditUIComponentUtil
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="value"></param>
        /// <param name="target"></param>
        /// <param name="options"></param>
        public static void UnitButton(string label, ref bool value, UnityEngine.Object target = null, Action onClick = null, params GUILayoutOption[] options)
        {
            bool isSelected = GUILayout.Toggle(value, label, "button", options);

            if (isSelected)
            {
                onClick?.Invoke();
            }


            if (value.Equals(isSelected)) return;



            value = isSelected;

            if (target == null) return;
            EditorUtility.SetDirty(target);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="text"></param>
        /// <param name="target"></param>
        /// <param name="options"></param>
        public static void UnitInputField(string label, ref string text, UnityEngine.Object target = null, params GUILayoutOption[] options)
        {
            GUILayout.BeginHorizontal("box");
            {
                GUILayout.Label(label);
                GUILayout.TextField(text);
            }
            GUILayout.EndHorizontal();

            if (target == null) return;
            EditorUtility.SetDirty(target);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="label"></param>
        /// <param name="value"></param>
        /// <param name="target"></param>
        /// <param name="options"></param>
        public static void UnitInputField(string label, ref int value, UnityEngine.Object target = null, params GUILayoutOption[] options)
        {
            GUILayout.BeginHorizontal("box");
            {
                GUILayout.Label(label);
                GUILayout.HorizontalSlider(value, 0, float.MaxValue);
            }
            GUILayout.EndHorizontal();

            if (target == null) return;
            EditorUtility.SetDirty(target);
        }
    }
}
